import {createStore} from 'vuex'

function saveSearchObjLocalStore (state) {
    window.sessionStorage.setItem("search",JSON.stringify(state.searchObj));
}

function savePagerLocalStore (state) {
    window.sessionStorage.setItem("pager",JSON.stringify(state.pager));
}


const store =  createStore({

    //数据
    state: {
        searchObj:JSON.parse(window.sessionStorage.getItem("search")) || {
            username:'',
            age:'',
            gender:'',

        },

        pager: JSON.parse(window.sessionStorage.getItem("pager")) ||  {pageNo:1, pageSize:10}
    },

    //方法
    mutations:{
        //保存查询条件
        saveSearchObj(state,searchObj){
            console.log("store",searchObj)
            state.searchObj.username = searchObj.username
            state.searchObj.age = searchObj.age
            state.searchObj.gender = searchObj.gender

            console.log("save",state.searchObj)
            window.sessionStorage.removeItem("search")
            saveSearchObjLocalStore(state)

            console.log(window.sessionStorage.getItem("search"))
        },

        //保存分页信息
        savePager(state,pager){
            console.log("store",pager)
            state.pager.pageNo = pager.page
            state.pager.pageSize = pager.limit
            window.sessionStorage.removeItem("pager")
            savePagerLocalStore(state)
        }
    },

    getters:{}
})
export default store